package com.glela.task;

import java.util.ArrayList;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.glela.cache.redis.RedisUtil;
import com.glela.goods.model.response.ResponseLimitSaleGoods;
import com.glela.goods.model.vo.limitsale.LimitSaleGoodsVo;
import com.glela.goods.service.LimitTimeSaleService;

public class GoodsTask {

	private static final Logger logger = LoggerFactory.getLogger(GoodsTask.class);

	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private LimitTimeSaleService limitTimeSaleService;
	public void canelLimitTimeSale(){
		ArrayList<Long> goodsIDs=new ArrayList<Long>();
		Set<String> StringKey2=redisUtil.selectKeysLike("temp:con:com:glela:controller:LimitTimeSaleController:getLimitSaleGoodsList:2*");//热卖
		Set<String> StringKey3=redisUtil.selectKeysLike("temp:con:com:glela:controller:LimitTimeSaleController:getLimitSaleGoodsList:3*");//预热
		for(String Key:StringKey2){
			try {
				JSONObject json=JSONObject.parseObject(redisUtil.get(Key));
				if(json.getString("code")!=null&&"10000".equals(json.getString("code"))){//查找成功
					ResponseLimitSaleGoods LimitSaleGoods=
						JSONObject.parseObject(json.get("data").toString(),ResponseLimitSaleGoods.class);
					for(LimitSaleGoodsVo goods:LimitSaleGoods.getGoodsList()){
						goodsIDs.add(goods.getGoodsId());
					}
					redisUtil.del(Key);	
				}
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
		}
		for(String Key:StringKey3){
			try {
				JSONObject json=JSONObject.parseObject(redisUtil.get(Key));
				if(json.getString("code")!=null&&"10000".equals(json.getString("code"))){//查找成功
					ResponseLimitSaleGoods LimitSaleGoods=
							JSONObject.parseObject(json.get("data").toString(),ResponseLimitSaleGoods.class);
					for(LimitSaleGoodsVo goods:LimitSaleGoods.getGoodsList()){
						goodsIDs.add(goods.getGoodsId());
					}
					redisUtil.del(Key);	
				}
			} catch (Exception e) {
				logger.error(e.getMessage());
			}
		}
		//删除产品缓存
		for(Long id:goodsIDs){
			logger.info("被删除的商品ID:{}", redisUtil.delKeysLike("temp:con:com:glela:controller:GoodsController:getGoodsInfoById:*"+id+"*"));
		}
	
	}
	protected RedisUtil getRedisUtil() {
		return redisUtil;
	}
}
